//
//  NNMockScanView.m
//  YHB_Prj
//
//  Created by fangshufeng on 2018/1/16.
//  Copyright © 2018年 striveliu. All rights reserved.
//

#import "NNMockScanView.h"

static CGFloat __scanAreaLength = 220;

@implementation NNMockScanView

- (id)init {
    
    self = [super init];
    
    if (self) {
        [self configSelf];
        [self allocSubviews];
        [self configSubviews];
    }
    
    return self;
}

- (void)configSelf {
    [self setBackgroundColor:[UIColor clearColor]];
}

- (void)allocSubviews {
    
    _returnBackButton = [[UIButton alloc] init];
    _scanLine = [[UIView alloc] init];
    _noteLabel = [[UILabel alloc] init];
}

- (void)configSubviews {
    
    /* Return Back Button */
    [_returnBackButton setTitle:@"返回" forState:UIControlStateNormal];
//    [_returnBackButton setTitleColor:MZLogColor(0x21996f3, 1) forState:UIControlStateNormal];
//    [_returnBackButton setTitleColor:MZLogColor(0x21996f3, 0.5) forState:UIControlStateHighlighted];
    [_returnBackButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [_returnBackButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    /* Scan Line */
//    [_scanLine setBackgroundColor:MZLogColor(0x2196f3, 0.8)];
    
    /* Note Label */
    NSString *noteStr =
    @"http://192.168.28.37:9778上面的二维码开启mock";
    [_noteLabel setText:noteStr];
    [_noteLabel setTextColor:[UIColor whiteColor]];
    [_noteLabel setFont:[UIFont systemFontOfSize:14]];
    [_noteLabel setTextAlignment:NSTextAlignmentCenter];
    [_noteLabel setNumberOfLines:0];
    
    [self addSubview:_returnBackButton];
    [self addSubview:_scanLine];
    [self addSubview:_noteLabel];
}

- (void)positionSubviews {
    
    CGRect tmpRect = CGRectZero;
    
    /* Return Back Button */
    tmpRect = CGRectMake(([UIScreen mainScreen].bounds.size.width - 80) / 2, 22, 80, 44);//[self rectInSubviewWithWidth:80 height:44 toLeft:0 toTop:22];
    [_returnBackButton setFrame:tmpRect];
    
    /* Scan Line */
    tmpRect = CGRectMake(([UIScreen mainScreen].bounds.size.width - 200) / 2,
                         ([UIScreen mainScreen].bounds.size.height - 2) / 2,
                         200, 2);
    [_scanLine setFrame:tmpRect];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self positionSubviews];
    [self setNeedsDisplay];
    
    /* Animation Reset */
    [_scanLine.layer removeAllAnimations];
    
    CGRect scanRect = [self scanRect];
    CGPoint tmpStartPoint = CGPointMake(scanRect.origin.x + scanRect.size.width / 2, scanRect.origin.y + 5);
    CGPoint tmpEndPoint = CGPointMake(scanRect.origin.x + scanRect.size.width / 2,
                                      scanRect.origin.y + scanRect.size.height - 5);
    CAAnimation *tmpAni = [self __animationWithDuration:2
                                             startPoint:tmpStartPoint
                                               endPoint:tmpEndPoint];
    [_scanLine.layer addAnimation:tmpAni forKey:nil];
}

#pragma mark - Draw Rect

- (void)drawRect:(CGRect)rect {
    
    /* 背景色 */
    [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] setFill];
    UIRectFill(rect);
    
    /* 透明选框 */
    CGRect tmpRect = [self scanRect];
    [[UIColor clearColor] setFill];
    UIRectFill(tmpRect);
    
    /* 四个角 */
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat tmpLength = 22;
    CGContextMoveToPoint(context, tmpRect.origin.x, tmpRect.origin.y);
    CGContextAddLineToPoint(context, tmpRect.origin.x + tmpLength, tmpRect.origin.y);
    
    CGContextMoveToPoint(context, tmpRect.origin.x + tmpRect.size.width - tmpLength, tmpRect.origin.y);
    CGContextAddLineToPoint(context, tmpRect.origin.x + tmpRect.size.width, tmpRect.origin.y);
    
    CGContextMoveToPoint(context, tmpRect.origin.x + tmpRect.size.width, tmpRect.origin.y);
    CGContextAddLineToPoint(context, tmpRect.origin.x + tmpRect.size.width, tmpRect.origin.y + tmpLength);
    
    CGContextMoveToPoint(context, tmpRect.origin.x + tmpRect.size.width,
                         tmpRect.origin.y + tmpRect.size.height - tmpLength);
    CGContextAddLineToPoint(context, tmpRect.origin.x + tmpRect.size.width, tmpRect.origin.y + tmpRect.size.height);
    
    CGContextMoveToPoint(context, tmpRect.origin.x + tmpRect.size.width, tmpRect.origin.y + tmpRect.size.height);
    CGContextAddLineToPoint(context, tmpRect.origin.x + tmpRect.size.width - tmpLength,
                            tmpRect.origin.y + tmpRect.size.height);
    
    CGContextMoveToPoint(context, tmpRect.origin.x + tmpLength, tmpRect.origin.y + tmpRect.size.height);
    CGContextAddLineToPoint(context, tmpRect.origin.x, tmpRect.origin.y + tmpRect.size.height);
    
    CGContextMoveToPoint(context, tmpRect.origin.x, tmpRect.origin.y + tmpRect.size.height);
    CGContextAddLineToPoint(context, tmpRect.origin.x, tmpRect.origin.y + tmpRect.size.height - tmpLength);
    
    CGContextMoveToPoint(context, tmpRect.origin.x, tmpRect.origin.y + tmpLength);
    CGContextAddLineToPoint(context, tmpRect.origin.x, tmpRect.origin.y);
    
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextStrokePath(context);
}

#pragma mark - Target-Action Pair

- (void)buttonClicked:(UIButton *)button {
    if ([_delegate respondsToSelector:@selector(scanViewReturnButtonCLicked:)]) {
        [_delegate scanViewReturnButtonCLicked:self];
    }
}

#pragma mark - MISC

- (CGRect)scanRect {
    
    return CGRectMake(([UIScreen mainScreen].bounds.size.width - __scanAreaLength) / 2,
                      ([UIScreen mainScreen].bounds.size.height - __scanAreaLength) / 2,
                      __scanAreaLength, __scanAreaLength);
}

#pragma mark - Animation

- (CAAnimation *)__animationWithDuration:(float)duration
                              startPoint:(CGPoint)startPoint
                                endPoint:(CGPoint)endPoint {
    
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    [moveAnimation setRemovedOnCompletion:YES];
    [moveAnimation setRepeatCount:100000];
    
    [moveAnimation setFromValue:[NSValue valueWithCGPoint:startPoint]];
    [moveAnimation setToValue:[NSValue valueWithCGPoint:endPoint]];
    [moveAnimation setDuration:duration];
    
    return moveAnimation;
}

@end
